package com.smart.tools;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonTool {

	 private ObjectMapper objectMapper = null;
	 
	 public JsonTool(){
		 
		 objectMapper = new ObjectMapper();
		 
	 }
	 
	 /**
	  * json字符串转换成Map集合
	  */
	 public Map<String, Map<String, Object>> readJson2Map(String json) {
		    try {
		        Map<String, Map<String, Object>> maps = objectMapper.readValue(json, Map.class);
		        return maps;
		    } catch (JsonParseException e) {
		        e.printStackTrace();
		    } catch (JsonMappingException e) {
		        e.printStackTrace();
		    } catch (IOException e) {
		        e.printStackTrace();
		    }
		    return null;
		}
	 
	 
	 /**
	  * json字符串转换成list<map>
	  * @return
	  */
	 public List<LinkedHashMap<String, Object>> readJson2List(String json) {
		    try {
		        List<LinkedHashMap<String, Object>> list = objectMapper.readValue(json, List.class);
		        return list;
		    } catch (JsonParseException e) {
		        e.printStackTrace();
		    } catch (JsonMappingException e) {
		        e.printStackTrace();
		    } catch (IOException e) {
		        e.printStackTrace();
		    }
		    return null;
		}
	 
	
}
